<template>
    <div article-container>
        <!-- 导航栏 -->
        <van-nav-bar 
            class="app-nav-bar"
            title="文章详情"
            left-arrow
            @click-left="$router.back()"
        >
        </van-nav-bar>
        <!-- /导航栏 -->

        <div class="rong">
            <h1 class="title">{{article.title}}</h1>
            <van-cell center class="user-info">
                <div slot="title" class="name">{{article.aut_name}}</div>
                <van-image
                    class="avatar"
                    fit="cover"
                    slot="icon"
                    round
                    :src="article.aut_photo"
                />
                <div slot="label" class="pubdate">
                    {{article.pubdate | relativeTime}}
                </div>
                <van-button 
                    class="follow-btn" 
                    :type="article.is_followed ? 'default' : 'info'" 
                    round 
                    size="small"
                    :icon="article.is_followed ? '' : 'plus'"
                    @click="onFollow"
                    :loading="isLoading"
                >
                    {{ article.is_followed ? '已关注' : '关注'}}
                </van-button>
            </van-cell>
            <div class="markdown-body">
                <div 
                    v-html="article.content"
                    ref="article-content"
                ></div>
            </div>
            <!-- 文章评论列表 -->
            <comment  
                :source="articleId"
                :list="commentList"
                @update-total-count="totalCommentCount = $event"
                @reply-click="onReplyClick"
            />
            <!-- /文章评论列表 -->

        </div>

        

        <!-- 底部区域 -->
        <div class="article-bottom">
            <van-button
                class="comment-btn"
                type="default"
                round
                @click="show=true"
                size="small"
            >写评论</van-button>
            <van-icon
                name="comment-o"
                :info="totalCommentCount"
                color="#777"
            />
            <van-icon
                color="#777"
                :name="article.is_collected ? 'star' : 'star-o'"
                @click="onGives"
            />
            <van-icon
                color="#777"
                :name="article.attitude === 1 ?'good-job' : 'good-job-o'"
                @click="onLike"
            />
            <van-icon 
                name="share" 
                color="#777777"
            >
            </van-icon>
        </div>
        <!-- /底部区域 -->

        <!-- 发布评论 -->
        <van-popup 
            v-model="show" 
            position="bottom"  
        >
            <post-comment 
                :target="articleId"
                @post-success="onPostSuccess"
            />
        </van-popup>
        <!-- /发布评论 -->

        <!-- 评论回复 -->
        <van-popup 
            v-model="isShow" 
            position="bottom"  
        >
            <CommentPeply 
                :comment="replyComment" 
                @close="isShow = false"
                v-if="isShow"
                :articleId="articleId"
            />
        </van-popup>
        <!-- /评论回复 -->
    </div>
</template>
<script>
import CommentPeply from './component/comment-reply.vue'
import postComment from './component/post-comment.vue'
import './github-markdown.css'
import { getArticleChannels } from '@/api/article'
import { ImagePreview } from 'vant'
import { addFollow,deleteFollow } from '@/api/user'
import { dianChannels , deleteChannels,shouChannels,deleteShouChannels } from '@/api/article'
import comment from './component/comment-list.vue'
export default {
    name:'ArticleIndex',
    data() {
        return {
            article:{},
            isLoading:false,
            isLoading1:false,
            show:false,
            commentList:[],
            totalCommentCount:0,
            isShow:false,
            replyComment:{}
        }
    },
    methods: {
        async loadArticle(){
            const { data } = await getArticleChannels(this.articleId)
            this.article = data.data
            //console.log(data)

            this.$nextTick(()=>{
                this.handlePerviewImage()
                //console.log(imgs)
            })
        },
        handlePerviewImage(){
            const articleContent = this.$refs['article-content']
            const imgs = articleContent.querySelectorAll('img')
            const imgPaths = []
            imgs.forEach((img,index)=>{
                imgPaths.push(img.src)
                img.onclick = function(){
                    ImagePreview({
                        images:imgPaths,
                        startPosition: index,
                    })
                }
            })
        },
        async onFollow(){
            this.isLoading1 = true
            if(this.article.is_followed){
                await deleteFollow(this.article.aut_id)
                
            }else{
               await addFollow(this.article.aut_id)
               
            }
            this.article.is_followed = !this.article.is_followed
            this.isLoading1 = false
        },
        async onGives(){
            this.isLoading = true
            if(this.article.is_collected){
                await deleteShouChannels(this.articleId)
                
            }else{
                await shouChannels(this.articleId)
                
            }
            this.article.is_collected = !this.article.is_collected
            this.isLoading = false
            this.$toast.success(`${this.article.is_collected ? '收藏成功':'取消收藏'}`)
        },
        async onLike(){
            this.$toast.loading({
                message:'操作中...',
                forbidClick:true
            })
            if(this.article.attitude === 1){
                await deleteChannels(this.articleId)
                this.article.attitude = -1
            }else{
                await dianChannels(this.articleId)
                this.article.attitude = 1
            }
            this.$toast.success(`${this.article.attitude ===1 ? '点赞成功':'取消点赞'}`)
        },
        onPostSuccess(comment){
            this.commentList.unshift(comment)

            this.show = false
        },
        onReplyClick(comment){

            this.replyComment = comment
            this.isShow = true
        }
    },
    created() {
        this.loadArticle()
    },
    props:{
        articleId:{
            type:[String,Number,Object],
            required:true
        }
    },
    components:{
        comment,
        postComment,
        CommentPeply
    }
}
</script>
<style lang="less" scoped>
.title{
    font-style: 20px;
    color: #3a3a3a;
    padding: 14px;
    background: #fff;
    margin: 0;
}
.rong{
    position: fixed;
    top: 46px;
    left: 0;
    right: 0;
    bottom: 44px;
    overflow-y: auto;
}
.user-info{
    .name{
        font-style: 12px;
        color: #333333;
    }
    .avatar{
        width: 35px;
        height: 35px;
        margin-right: 8px;
    }
    .pubdate{
        font-size: 11px;
        color: #b4b4b4;
    }
    .follow-btn{
        width: 85px;
        height: 29px;
    }
}

.markdown-body{
    padding: 14px;
    background: #fff;
}
.article-bottom {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    justify-content: space-around;
    align-items: center;
    box-sizing: border-box;
    height: 44px;
    border-top: 1px solid #d8d8d8;
    background-color: #fff;
    .comment-btn {
      width: 141px;
      height: 23px;
      border: 1px solid #eeeeee;
      font-size: 15px;
      line-height: 23px;
      color: #a7a7a7;
    }
    .van-icon {
      font-size: 20px;
      .van-info {
        font-size: 11px;
        background-color: #e22829;
      }
    }
  }
</style>